﻿// Copyright 2012 Jack Bulan (agentgeo)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

using GemWorkshop.Formats.Nitro;
using GemWorkshop.Pokemon;

namespace GemWorkshop.Pokedex
{
    public partial class DexOrderEditor : Form
    {
        PokemonLoader pl;
        Narc snr;
        ushort[] currentDex;

        public DexOrderEditor()
        {
            InitializeComponent();

            snr = new Narc(Form1.GameDirectory + "\\" + Form1.GameSettings.ReadValue(Form1.Game, "PokedexData"));
            pl = new PokemonLoader(null, null, null);

            cPoke.Items.Clear();
            for (int i = 0; i < 500; i++) // 500 pokemon
            {
                cPoke.Items.Add(pl.GetName(i));
            }

            ReadDex();
            ListDex();
        }

        private void bSave_Click(object sender, EventArgs e)
        {
            WriteDex();
        }

        private void bCross_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void listOrder_SelectedIndexChanged(object sender, EventArgs e)
        {
            cPoke.SelectedIndex = currentDex[listOrder.SelectedIndex];
        }

        private void bEdit_Click(object sender, EventArgs e)
        {
            currentDex[listOrder.SelectedIndex] = (ushort)cPoke.SelectedIndex;
            int index = listOrder.SelectedIndex;
            ListDex();
            listOrder.SelectedIndex = index;
            //listOrder.SelectedItem = pl.GetName(cPoke.SelectedIndex);
        }

        private void ReadDex()
        {
            BinaryReader br = new BinaryReader(snr.ExtractStream(0));

            currentDex = new ushort[152];
            for (int i = 0; i < 152; i++) // 152 in dex
            {
                ushort value = br.ReadUInt16();
                currentDex[i] = value;
            }

            br.Close();
            br.Dispose();
        }

        private void WriteDex()
        {
            MemoryStream mem = snr.ExtractStream(0);
            BinaryWriter bw = new BinaryWriter(mem);

            foreach (ushort u in currentDex)
            {
                bw.Write(u);
            }

            // Save to file.
            //NarcWriter nw = new NarcWriter(snr);
            //nw.ReplaceFile(0, mem);
            snr.ReplaceFile(0, mem);

            bw.Close();
            bw.Dispose();
            mem.Close();
            mem.Dispose();
        }

        private void ListDex()
        {
            listOrder.Items.Clear();
            foreach (ushort u in currentDex)
            {
                listOrder.Items.Add(pl.GetName(u));
            }
        }
    }
}
